#!/usr/bin/env bash

:<<description

功能：自动生成 unit 执行计划
描述：

description

#设置脚本异常立即退出
set -e
#获取当前脚本名
script=$0
scriptBase=$(basename ${script})
scriptDir=$(cd $(dirname $0); pwd)
workDir=${scriptDir}


function compose () {

    composeDir=${workDir}/node/compose
    shDir=${workDir}/node

    for item in $(cd ${composeDir}; ls -d */*); do

        dir1=$(echo $item | cut -d '/' -f1)
        dir2=$(echo $item | cut -d '/' -f2)

        mkdir -p ${shDir}/${dir1}
        shFile=${shDir}/${dir1}/${dir2}.sh
        if [ -f ${shFile} ]; then

            rm -f ${shFile}
        fi

        mapfile components < ${composeDir}/${dir1}/${dir2}/main.sh
        for (( i=0; i < ${#components[@]}; i++ )); do
            #每一行
            component=${components[${i}]}
            #解析跳过空行
            if (( `echo ${component} | sed -n -E '/^$/p' | wc -l` == 1  )); then continue; fi #空行
            #解析跳过注释
            if (( `echo ${component} | sed -n -E '/^#/p' | wc -l` == 1  )); then continue; fi #注释

            if [ -f ${composeDir}/${component} ]; then componentFile=${workDir}/node/compose/${component}; fi
            if [ -f ${composeDir}/${dir1}/${dir2}/${component} ]; then componentFile=${workDir}/node/compose/${dir1}/${dir2}/${component}; fi

            cat ${componentFile} >> ${shFile}
            echo -e '\n\n' >> ${shFile}

        done

        chmod a-w ${shFile}
        ls -alh ${shFile}

    done

}

compose




